﻿/* Nibiru Studio Engine Version [1070] */
#include "EYE.h"
#include "Apis/NEngineApi.h"

ENGINE_NAMESPACE_BEGIN

IMPLEMENT_CLASS(EYE)
IMPLEMENT_RTTI_CUSTOM(EYE, NComponent)

//ENUM_BEGIN(LabelType, LabelType)
// add your enum
// Display Name, Enum Type Name
// ENUM(Left, LEFT)
//ENUM_END


REFLECTION_BEGIN(EYE)
// add your variable reflection property
//Enum Class Name, Display Name In Editor, Variable Name In Class, flags default is 0
//ENUM_PROPERTY(LabelType, Type, m_LabelType, 0)
//PROPERTY(Bold, m_IsBold, 0)
//PROPERTY(Rotate, m_EnableRotate, 0)
//FUNCTION(OnButtonClick, EYE, OnClick)

REFLECTION_END


void EYE::Start()
{
	startPoint = Vector3::Zero; //确定射线起点
    endPoint = Vector3::One * 0.2f; //确定射线终点



//	readCsv(filePath);
	file = std::ifstream(filePath); //打开射线文件
	if (!file.is_open()) {
		std::cerr << "Unable to open file: " << filePath << std::endl;
		return;
	}

//	获取模拟眼动焦点的对象
	m_sphere_start = NActorManager::GetActor("Sphere_start");
	Vector3 scale = m_sphere_start->GetLocalScale();
	startPoint = m_sphere_start->GetLocalPosition(); //确定起点位置


	m_sphere_end = NActorManager::GetActor("Sphere_end");
	endPoint = m_sphere_end->GetLocalPosition(); //确定终点

}

void EYE::Update()
{
//获取世界坐标系下的旋转平移矩阵
	Quaternion q_left_camera = NCamera::XRLeftEyeCamera()->GetOrientation(); //获取相机actor的旋转四元数
	NDebug::Log("main camera Q is %f %f %f %f\n",q_left_camera.x, q_left_camera.y, q_left_camera.z, q_left_camera.w);

	Tmpl_Matrix4 mainCameraRot = q_left_camera.ToRotationMatrix(); //获取相机actor的旋转矩阵
	NDebug::Log("main camera rotation is\n"
				"%f %f %f\n"
				"%f %f %f\n"
				"%f %f %f\n",
				mainCameraRot[0][0], mainCameraRot[0][1], mainCameraRot[0][2],
				mainCameraRot[1][0], mainCameraRot[1][1], mainCameraRot[1][2],
				mainCameraRot[2][0], mainCameraRot[2][1], mainCameraRot[2][2]);

	mainCameraPos = NCamera::XRLeftEyeCamera()->GetPosition(); //获取相机actor的位置，3dof模式，位移数据为0
	NDebug::Log("main camera position is %f %f %f\n", mainCameraPos.x, -mainCameraPos.y, mainCameraPos.z);


	startPoint = m_sphere_start->GetLocalPosition(); //确定起点位置
	NDebug::Log("m_sphere_start local position is %f %f %f\n", startPoint.x, startPoint.y, startPoint.z);


	// Called every frame if actor is enabled.
//	NGraphics::DrawLine(startPoint, endPoint, Color::Green); //绘制射线

//	float timeTuch = NTime::GetDeltaTime();
//	NDebug::Log("======touch time is %f=====\n",timeTuch);


	if(std::getline(file, line)) //读取每一行
	{
		std::istringstream iss(line);
		std::string cell;
		std::vector<float> row;
		while (std::getline(iss, cell, ',')) {
			// 在这里可以添加代码来处理或清理单元格内容
			row.push_back(std::stof(cell));
		}
		dirPoint = {row[0], -row[1], -row[2]}; //获取眼动方向
		NDebug::Log("======read frame data=====\n");

		//	计算射线方向，在旋转平移作用后的新坐标
//		Vector3 newDirPoint;
//		newDirPoint.x = dirPoint.x * mainCameraRot[0][0] + dirPoint.y * mainCameraRot[1][0] + dirPoint.z * mainCameraRot[2][0] + mainCameraPos.x;
//		newDirPoint.y = dirPoint.x * mainCameraRot[0][1] + dirPoint.y * mainCameraRot[1][1] + dirPoint.z * mainCameraRot[2][1] + mainCameraPos.y;
//		newDirPoint.z = dirPoint.x * mainCameraRot[0][2] + dirPoint.y * mainCameraRot[1][2] + dirPoint.z * mainCameraRot[2][2] + mainCameraPos.z;

//		Vector3 newStartPoint;
//		newStartPoint.x = startPoint.x * mainCameraRot[0][0] + startPoint.y * mainCameraRot[1][0] + startPoint.z * mainCameraRot[2][0] + mainCameraPos.x;
//		newStartPoint.y = startPoint.x * mainCameraRot[0][1] + startPoint.y * mainCameraRot[1][1] + startPoint.z * mainCameraRot[2][1] + mainCameraPos.y;
//		newStartPoint.z = startPoint.x * mainCameraRot[0][2] + startPoint.y * mainCameraRot[1][2] + startPoint.z * mainCameraRot[2][2] + mainCameraPos.z;
//		m_sphere_end->SetPosition(newStartPoint);


		endPoint = startPoint + dirPoint;
		m_sphere_end->SetLocalPosition(endPoint); //设置局部坐标系的位置

		startPoint = m_sphere_start->GetPosition();//获取世界坐标系起点位置
		endPoint = m_sphere_end->GetPosition(); //获取世界坐标系的终点位置


		NGraphics::DrawLine(startPoint, endPoint, Color::Red);


	}else
	{
		file.close();
		NDebug::Log("======read frame data end======\n");

		file = std::ifstream(filePath); //打开射线文件
		if (!file.is_open()) {
			std::cerr << "Unable to open file: " << std::endl;
			return;
		}


	}

//	退出键退出应用
	if(NInput::GetKeyDown(KeyboardKeys::Android_Back))
	{
		NApplication::Quit();
	}

//	mainCameraPos = NCamera::MainCamera()->GetPosition(); //获取相机actor的位置[0,0,0]


//	auto cameraAct = NActorManager::GetActor("MainCamera"); //获取相机actor
//	mainCameraPos = cameraAct->GetCamera()->GetPosition(); //获取相机actor的位置[0,0,0]
//	mainCameraPos = cameraAct->GetComponent<NTransformComponent>()->GetLocalPosition(); //通过相机actor的变换组件获取相机位置







}

void EYE::OnDestroy()
{
	// When the component deleted
}

void EYE::PostEditChangeProperty(std::string& propertyName)
{
	// Variable Property changed in Editor
}

void EYE::Serialize(Archive& ar)
{
	Super::Serialize(ar);
	// SERIALIZE(m_EnableRotate);
}

void EYE::LateDeserialize(Archive& ar)
{
	// After all Serialize are executed
	Super::LateDeserialize(ar);
}

void EYE::readCsv(const std::string& filePath) {
	std::ifstream file(filePath);
	if (!file.is_open()) {
		std::cerr << "Unable to open file: " << filePath << std::endl;
		return;
	}

	std::string line;
	while (std::getline(file, line)) {
		std::istringstream iss(line);
		std::string cell;
		std::vector<std::string> row;

		while (std::getline(iss, cell, ',')) {
			// 在这里可以添加代码来处理或清理单元格内容
			row.push_back(cell);
		}

		// 输出整行内容
		for (const auto& cell : row) {
			std::cout << cell << " ";
		}
		std::cout << std::endl;
	}

	file.close();
}

ENGINE_NAMESPACE_END
